﻿using DataServer.Entities;
using IsPay.Logic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IsPay.View
{
    public partial class AccuntBindUser : System.Web.UI.Page
    {
        private String returnmsge = "";
        public IEnumerable<Accunt> lsAccunt=new List<Accunt>();
        private IEnumerable<UserProfile> lsuser= new List<UserProfile>();
        private IEnumerable<UserProfile> lsuserusign = new List<UserProfile>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] == null || Session["USERID"].ToString().Length <= 0)
            {
                Response.Write("<script>top.location='../Default.aspx'</script>");
            }
            AccuntSer aser = new AccuntSer();
            lsAccunt =aser.QueryAccunt("", "", "True");
            if (Request.Params["ASKTYPE"] != null && Request.Params["ASKTYPE"].ToString().Length >= 0)
            {
                String strasktype = Request.Params["ASKTYPE"].ToString();
                if (strasktype.Equals("AJAX"))
                {
                    String stracid = Request.Params["SELACCUNTID"].ToString();
                    if (stracid.Length <= 0)
                    {
                        Response.Write("ERROR");
                    }
                    else
                    {
                        lsuser = new List<UserProfile>();
                        UserSer user = new UserSer();
                        lsuser = user.GetUsersByAccuntID(stracid);
                        lsuserusign = user.GetUsersNotSignAccunt();
                        String strsignuser = JsonConvert.SerializeObject(lsuser);
                        String strunsignuser = JsonConvert.SerializeObject(lsuserusign);
                        JObject jo = new JObject();
                        jo.Add("SINGNUSER", strsignuser);
                        jo.Add("UNSINGNUSER", strunsignuser);
                        String strreturn = jo.ToString();
                        Response.Write(strreturn);
                        Response.End();
                    }
                }else if (strasktype.Equals("EDIT"))
                {
                    String edittablejasn = Request.Params["updatejason"].ToString().Trim();
                    String straccuntid = Request.Params["accuntid"].ToString().Trim();
                    if (straccuntid.Length <= 0)
                    {
                        lblMsg.Text = "请选择账号";
                        return;
                    }
                    DataTable dtedit = CommonFormat.JasonToDataTable(edittablejasn);
                    if (dtedit != null && dtedit.Rows.Count > 0)
                    {
                        List<DataServer.Entities.UserProfile> lsadd = new List<DataServer.Entities.UserProfile>();
                        List<DataServer.Entities.UserProfile> lsdel = new List<DataServer.Entities.UserProfile>();
                        DataRow[] dradds = dtedit.Select(" ADDFLAG=true AND DELETEFLAG=false ");                        
                        DataRow[] drdels = dtedit.Select(" DELETEFLAG=true ");
                        lsadd = DataRowToUser(dradds,straccuntid);                      
                        lsdel = DataRowToUser(drdels,"");
                        UserSer user = new UserSer();
                        String strmsg = user.UpdateUserAccunt(lsadd, lsdel, Session["USERID"].ToString());
                        if (strmsg.Equals("OK"))
                        {
                            lblMsg.Text = "更新成功";
                        }
                        else
                        {
                            lblMsg.Text = strmsg;
                        }
                    }
                }
            }


        }
        private List<DataServer.Entities.UserProfile> DataRowToUser(DataRow[] drs,String straccuntid)
        {
            List<DataServer.Entities.UserProfile> lsuser = new List<DataServer.Entities.UserProfile>();
            foreach (DataRow dr in drs)
            {
                DataServer.Entities.UserProfile user = new DataServer.Entities.UserProfile();
                user.UserId = dr["USERID"].ToString();
                user.UserName = dr["USERNAME"].ToString();
                user.AccuntId = straccuntid;
                user.LastLoginTimeKey = "";
                user.LastPCIp = Request.UserHostAddress;
                user.LastPCName = Request.UserHostName;
                user.LastPCMac = "";
                lsuser.Add(user);
            }

            return lsuser;
        }
    }
}